/*
 * can.h
 *
 *  Created on: 2022年8月5日
 *      Author: pengkun.he
 */

#ifndef SRC_CAN_CAN_H_
#define SRC_CAN_CAN_H_

/*CAN总线标志量*/
#define CAN_SINGLE 0
#define CAN_MULTIPLE 1
#define CAN_YC_FDT 1
#define CAN_GB_FDT 2
#define CAN_GPS_FDT 5
#define CAN_BDS_FDT 7
#define CAN_TLEGPS_FDT 8
#define CAN_TLEBDS_FDT 9
#define CAN_ORBIT_FDT 3
#define CAN_ACS_FDT 4
#define CAN_INDIRECT 4
#define CAN_GNSS_MAX 500
#define CAN_TLE_MAX 300
#define CAN_ACS_MAX 300
#define CAN_ORBIT_MAX 100
#define GNSS_VID 0x81
#define ACS_VID 0x04


#define CAN_YC_MAX (27+8)
#define CAN_YC_NUM 27
#define CAN_GB_MAX (62+8)
#define CAN_GB_NUM 62
#define   CAN_YC_ROW   CAN_YC_MAX/7
#define   CAN_YC_MOD   CAN_YC_MAX%7  //0或大于0,判断余数
#define   CAN_GB_ROW   CAN_GB_MAX/7
#define   CAN_GB_MOD   CAN_GB_MAX%7  //0或大于0,判断余数

#if CAN_YC_MOD
    #define CAN_YC_INDEX    CAN_YC_ROW+1
#else
    #define CAN_YC_INDEX    CAN_YC_ROW
#endif


#if CAN_GB_MOD
    #define CAN_GB_INDEX    CAN_GB_ROW+1
#else
    #define CAN_GB_INDEX    CAN_GB_ROW
#endif




/*CAN总线各地址11位ID判别*/
struct canbus_ID {
	int telemeter_id;     //轮询遥测、广播
	int gnss_id;   //GNSS广播
	int tle_id;   //GNSS星历广播
	int starsensor_id;   //姿控星敏广播
	int indirect_id;  //间接指令
	int action_id;  //内务指令
	int orbit_id;  //ACS轨道广播
};



void *thread_can_0(void *argv);
void *thread_can_1(void *argv);

void *thread_can_2(void *argv);
void *thread_can_3(void *argv);



/*CAN总线轮询遥测、广播应答函数*/
void senddata_normal(int data_type, int send_fd, struct sockaddr *addr_yc, int addr_size);
void senddata_normal_1(int data_type, int send_fd, struct sockaddr *addr_yc, int addr_size);
int telemeter_recvsum(struct can_frame *recieve);
int telemeter_recvsum_1(struct can_frame *recieve);

int indirect_recvsum(struct can_frame *recieve);
void senddata_unnormal(int send_fd, struct sockaddr *addr_yc, int addr_size);

int action_recvsum(struct can_frame *recieve);
void senddata_action(int send_fd, struct sockaddr *addr_nw, int addr_size, unsigned char action_fdt);

void multiple_gnssdata(struct can_frame *recvdata, int type_vId);
void multiple_tledata(struct can_frame *recvdata, int type_vId);
void multiple_acsdata(struct can_frame *recvdata, int type_vId);
void multiple_orbitdata(struct can_frame *recvdata, int type_vId);

void acsdata_write(const char *filename, unsigned char *data, int len);



#endif /* SRC_CAN_CAN_H_ */
